package info.mhylle.mediaplayer.data;

import info.mhylle.mediaplayer.data.model.Album;
import info.mhylle.mediaplayer.data.utils.SessionFactoryUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.List;

public class DataManager {

    public DataManager() {

    }

    public void saveAlbum(Album album) {
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.save(album);
        transaction.commit();
    }

    public void updateAlbum(Album album) {
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.update(album);
        transaction.commit();
    }

    public void deleteAlbum(Album album) {
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.delete(album);
        transaction.commit();
    }

    public Album getAlbum(long albumId) {
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Album album = (Album) session.get(Album.class, albumId);
        transaction.commit();
        return album;
    }

    public java.util.List<Album> getAlbumsByReleaseYear(int minReleaseyear) {
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        Transaction transaction = session.beginTransaction();
        org.hibernate.Query q = session.createQuery("from Album u where u.releaseyear >= :minReleaseyear");
        q.setInteger("minReleaseyear", minReleaseyear);
        List list = q.list();
        transaction.commit();
        return list;
    }

	public java.util.List<Album> getAlbums() {
		Session session = SessionFactoryUtil.getInstance().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		org.hibernate.Query q = session.createQuery("from Album u");
		List list = q.list();
		transaction.commit();
		return list;
	}
}
